package cn.csdj.sism.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {//HandlerInterceptor是SpringMVC提供的拦截器接口，用于拦截请求，实现HandlerInterceptor接口，重写三个方法。
    /**
     * 目标方法执行之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURI = request.getRequestURI();
        log.info("preHandle拦截的请求路径是{}",requestURI);

        //登录检查逻辑
        HttpSession session = request.getSession();

        Object success = session.getAttribute("success");

        if(success!= null && success.equals(1)){
            //放行
            return true;
        }

        //拦截住。未登录。跳转到登录页
        response.sendRedirect("http://localhost:8080/#/login");
        return false;
    }

}
